﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Apmis2._1.Models;

namespace Apmis2._1.Controllers
{
    [RoutePrefix("api/Patient")]
    public class RegNoController : ApiController
    {
        private IPatientRepository _repo;
        public RegNoController(IPatientRepository Repo)
        {
            _repo = Repo;
        }

        [HttpGet]
        [Route("{patientID:guid}/RegNo")]
       public IHttpActionResult getRegNo(Guid patientID)
       {
           var query = _repo.GetRegNoByPatientID(patientID);
           if (query != null)
           {
               return Ok(query);
           }
           return NotFound();
       }

        [HttpPost]
        [Route("{patientID:guid}/RegNo")]
        public IHttpActionResult Post(Guid Patientid, [FromBody]RegNo newRegNo)
        {

            newRegNo.PatientId = Patientid;
           // newRegNo.RegLocation = Patientid;
            
            if (_repo.addRegNo(newRegNo) &&
                _repo.save())
            {
                //return //Request.CreateResponse(HttpStatusCode.Created, newpatient);
                //Created(System.uri,newPatient);

                return Created<RegNo>(Request.RequestUri + newRegNo.Id.ToString(), newRegNo);
            }
            else
            {

                return BadRequest();
            }

        }
    }
}
